using System;
using System.Drawing;
using System.Windows.Forms;
namespace eTMS.Controls
{
	/// <summary>
	/// Summary description for ComboBox.
	/// </summary>
    public class ComboBox : System.Windows.Forms.ComboBox
	{
        public event EventHandler OnPressEnter;
        public event EventHandler OnPressLeft;
        public event EventHandler OnPressRight;
        public event EventHandler OnPressUp;
        public event EventHandler OnPressDown;
        public event EventHandler OnPressPageDown;
        public event EventHandler OnPressPageUp;
        private Control _PrevText;
        private Control _NextText;
        public Control PrevText
        {
            set
            {
                _PrevText = value;
            }
            get
            {
                return _PrevText;

            }
        }
        public Control NextText
        {
            set
            {
                _NextText = value;
            }
            get
            {
                return _NextText;

            }
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (OnPressEnter != null) OnPressEnter(this, e);
                    if (_NextText != null) _NextText.Focus();
                    if (_NextText is Button)
                    {
                        ((Button)_NextText).PerformClick();
                        e.Handled = true;
                    }
                    
                    break;
                case Keys.Left:
                    if (OnPressLeft != null) OnPressLeft(this, e);
                    if ((_PrevText != null) && (this.SelectionStart == 0)) _PrevText.Focus();
                    break;
                case Keys.Right:
                    if (OnPressRight != null) OnPressRight(this, e);
                    if ((_NextText != null) && (this.SelectionStart == this.Text.Length)) _NextText.Focus();
                    break;
                case Keys.Up:
                    if (OnPressUp != null) OnPressUp(this, e);
                    break;
                case Keys.Down:
                    if (OnPressDown != null) OnPressDown(this, e);
                    break;
                case Keys.PageDown:
                    if (OnPressPageDown != null) OnPressPageDown(this, e);
                    break;
                case Keys.PageUp:
                    if (OnPressPageUp != null) OnPressPageUp(this, e);
                    break;
            }
        }
	
        public ComboBox()
        {
            mclrBorder = Color.FromArgb(140, 180, 205);
            mclrHighlight = Color.FromArgb(255, 205, 120);
        }

        private Color mclrBorder;
        private Color mclrHighlight;
        public Color BorderColor
        {
            get
            {
                return mclrBorder;
            }
            set
            {
                mclrBorder = value;
            }
        }
        private eTMS.Controls.TextBox txtValue;
		
		protected override void OnTextChanged(EventArgs e)
		{
			base.OnTextChanged (e);
			if(this.txtValue!=null){
				txtValue.Text=this.Text;
			}
		}

		private bool bReadOnly=false;
		public bool ReadOnly
		{
			get
			{
				return bReadOnly;
			}
			set
			{
				bReadOnly=value;
				if(bReadOnly){
					if(txtValue!=null)txtValue.Dispose();
                    txtValue = new eTMS.Controls.TextBox();
					this.Controls.Add(txtValue);
					txtValue.Font=this.Font;
					txtValue.Multiline=true;
					txtValue.Width=this.Width;
					txtValue.Height=this.Height;
					txtValue.Location=new Point(0,0);
					txtValue.Text=this.Text;
					txtValue.ReadOnly=true;					
				}
				else
				{
					if(txtValue!=null){
						this.Controls.Remove(txtValue);
						txtValue.Dispose();
					}
				}
			}
		}
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);
            if (m.Msg != eTMS.Commons.Win32.WM_PAINT)
            {
                return;
            }
            if (bReadOnly)
                return;
            Rectangle rec = this.ClientRectangle;
            Pen pen = null;
            if (!DesignMode && (!ReadOnly) && Enabled && (Focused))
                pen = new Pen(mclrHighlight);
            else
                pen = new Pen(mclrBorder);
            IntPtr hDc = eTMS.Commons.Win32.GetWindowDC(Handle);
            Graphics g = Graphics.FromHdc(hDc);
            g.DrawRectangle(pen, 0, 0, rec.Width - 1, rec.Height - 1);
            g.Dispose();
            pen.Dispose();
            eTMS.Commons.Win32.ReleaseDC(Handle, hDc);
        }

	}
}
